package utility;

import org.xml.sax.*;

public class SaxIntermediary implements ContentHandler {

   private ContentHandler handler;
   private int indentation = 0;
   
   public SaxIntermediary (ContentHandler handler) {
      this.handler = handler;
   }
   
   private void print (String message) {
      for (int i = 0; i < indentation; ++i) {
         System.out.print(" ");
      }
      
      System.out.println(message);
   }

   public void characters(char[] ch, int start, int length) throws SAXException {
      handler.characters(ch, start, length);
      
      StringBuilder builder = new StringBuilder();
      for (int i = start; i < start + length; ++i) {
         if (ch[i] == '\n')
            builder.append("\\n");
         else if (ch[i] == '\r')
            builder.append("\\r");
         else if (ch[i] == '\t')
            builder.append("\\t");
         else
            builder.append(ch[i]);
      }
      
      print("characters(" + builder.toString() + ")");
   }

   public void endDocument() throws SAXException {
      handler.endDocument();
      indentation -= 3;
      print("endDocument()");
   }

   public void endElement(String uri, String localName, String qName)
         throws SAXException {
      handler.endElement(uri, localName, qName);
      indentation -= 3;
      print("endElement()");
   }

   public void endPrefixMapping(String prefix) throws SAXException {
      handler.endPrefixMapping(prefix);
      indentation -= 3;
      print("endPrefixMapping(" + prefix + ")");
   }

   public void ignorableWhitespace(char[] ch, int start, int length)
         throws SAXException {
      handler.ignorableWhitespace(ch, start, length);
      print("ignorableWhitespace(...)");
   }

   public void processingInstruction(String target, String data)
         throws SAXException {
      handler.processingInstruction(target, data);
      print("processingInstruction(...)");
   }

   public void setDocumentLocator(Locator locator) {
      handler.setDocumentLocator(locator);
      print("setDocumentLocator(...)");
   }

   public void skippedEntity(String name) throws SAXException {
      handler.skippedEntity(name);
      print("skippedEntity(" + name + ")");
   }

   public void startDocument() throws SAXException {
      handler.startDocument();
      print("startDocument(...)");
      indentation += 3;
   }

   public void startElement(String uri, String localName, String qName,
         Attributes atts) throws SAXException {
      handler.startElement(uri, localName, qName, atts);
      
      StringBuilder builder = new StringBuilder();
      builder.append("startElement(");
      builder.append("<");
      builder.append(qName);
      builder.append(" ");
      
      for (int i = 0; i < atts.getLength(); ++i) {
         builder.append(atts.getURI(i));
         builder.append(":" + atts.getLocalName(i));
         builder.append("=\"");
         builder.append(atts.getValue(i));
         builder.append("\" ");
      }
      
      builder.append(">)" );
      builder.append("URL: " + uri + ", ");
      builder.append("localName: " + localName);
      print(builder.toString());
      indentation += 3;
   }

   public void startPrefixMapping(String prefix, String uri)
         throws SAXException {
      handler.startPrefixMapping(prefix, uri);
      print("startPrefixMapping(" + prefix + ", " + uri + ")");
      indentation += 3;
   }
   
   

}
